# -*- coding:UTF8 -*-
# author:叶向平
# date:2020/4/5
# IDE:PyCharm

import requests,json
import base64
import time

'''
在线活体检测
access_token获取:access_token.py(每一个月更新一次)
'''

def post():
    live_face = []
    yaw_list = []

    for i in range(10):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceverify"
        s = ''
        with open("./img/now.jpg", 'rb') as f:
            base64_data = base64.b64encode(f.read())
            s = base64_data.decode()
        params = [{"image":s,"image_type":"BASE64"}]
        params = json.dumps(params)

        access_token = '24.c2caabcdef0140be9c76f8601a55cdea.2592000.1588664452.282335-15225271'
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        try:
            response = requests.post(request_url, data=params, headers=headers)
            # print(response.json())
            live_face.append(response.json()['result']['face_liveness'])
            yaw_list.append(response.json()['result']['face_list'][0]['angle']['yaw'])
        except Exception as e:
            print(e)
        time.sleep(0.2)

    # print(live_face,yaw_list)
    if max(live_face) > 0.9 and (max(yaw_list)-min(yaw_list)>40):
        return True
    else:
        return False